﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleSurvival.src
{
    //-----------------------------------------------//
    //CLASS - EntityDefinitions.cs
    //-----------------------------------------------//
    //Only to be used in constructors
    //Contains lists of construction data for entitys
    //Kind of like a factory Pattern in retrospect
    //Loads data from a txt file
    public class EntityDefinitions
    {
        //Name of fields in the data file with thier position
        public enum FieldName
        {
            ENTITY_ID = 0,
            ENTITY_NAME = 1,
            ENTITY_IMAGE_PATH = 2,
            ENTITY_TYPE = 3,
            ENTITY_SPEED = 4,
            UNIT_HP = 5,
            TERRAIN_PATHABLE = 5,
            TERRAIN_ISWATER = 6,
        }

        public const int MAX_ENTITY_DEFINITIONS = 50;

        //links entity names to thier EntityDefinitionId
        public Dictionary<string, int> _entityNames = 
            new Dictionary<string, int>(MAX_ENTITY_DEFINITIONS);

        //data that was gathered from files
        List<string[]> _entityData = new List<string[]>(MAX_ENTITY_DEFINITIONS);   

        //Points to data for terrain
        List<int> _terrainIds = new List<int>();
        public List<int> TerrainDefIds
        {
            get { return _terrainIds; }
        }

        //Points to data for units
        List<int> _unitIds = new List<int>();
        public List<int> UnitDefIds
        {
            get { return _unitIds; }
        }

        //Points to data for any entity
        public int GetEntityDefId(string name) { return _entityNames[name]; }

        public int EntityDefIdCount { get { return _entityData.Count; } }

        public string GetEntityName(int id)
        {
            return _entityData[id][(int)FieldName.ENTITY_NAME]; 
        }

        //Returns default values given the fieldname and entity id
        public string GetEntityDefValue(int id, FieldName fieldName)
        {
            string val = null;
            val = _entityData[id][(int)fieldName];

            if (val == null) { Console.WriteLine(
                "EntityDefinitions.GetValue - Error - Field not found"); }

            return val;
        }

        //With a string
        public string GetEntityDefValue(string entityName, FieldName fieldName)
        {
            int id = _entityNames[entityName];
            return GetEntityDefValue(id, fieldName);
        }

        //Contructor loads the file and imports all data into _entityData
        public EntityDefinitions() { LoadDefinitions(); }

        public void LoadDefinitions()
        {
            _entityData = ScriptReader.GetData("EntityDefinitions.txt");
            foreach (string[] s in _entityData)
            {
                int id = Convert.ToInt16(s[(int)FieldName.ENTITY_ID]);
                string name = s[(int)FieldName.ENTITY_NAME];
                if (s[(int)FieldName.ENTITY_TYPE] == "TERRAIN")
                {
                    _terrainIds.Add(id);
                }
                else if (s[(int)FieldName.ENTITY_TYPE] == "UNIT")
                {
                    _unitIds.Add(id);
                }
                _entityNames.Add(name, id);
            }
        }
    }
}
